<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="500px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="100px" :rules="formRules">                
                <el-form-item label="开始时间" prop="startTime">				
					<el-time-select				
					    v-model="formData.startTime"
						start = "08:30"
						step= "00:30"
						end = "23:00"
					    clearable					    
					    placeholder="请选择开始时间"
					/>					
                </el-form-item>
								
                <el-form-item label="结束时间" prop="endTime">
					<el-time-select
					  v-model="formData.endTime"					  
					  start = "08:30"
					  step= "00:30"
					  end = "23:30"
					  :min-time="formData.startTime"
					  clearable
					  placeholder="请选择结束时间">
					</el-time-select>
                </el-form-item>  
				
				<el-form-item label="附加金额" prop="orderAmount">
				    <el-input-number v-model="formData.orderAmount" :min="0" :max="9999" />				    
				</el-form-item>
				
				<el-form-item label="排序" prop="sort">				    
				    <el-input-number v-model="formData.sort" :min="0" :max="9999" />				    
				</el-form-item>
				
				<el-form-item
				    label="状态"
				    prop="isDisable"
				    required
				>
				    <div>
				        <el-radio-group v-model="formData.isDisable">
				            <el-radio :label="0">开启</el-radio>
				            <el-radio :label="1">关闭</el-radio>
				        </el-radio-group>
				        
				    </div>
				</el-form-item>
							   
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {  slotEdit, slotAdd, slotDetail } from '@/api/setting/slot'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑预约时间' : '新增预约时间'
})

const formData = reactive({
    id: '',
    startTime: '',
    endTime: '',
	orderAmount: 0,
    sort: '',
    isDisable: 0,
})

const formRules = {    
    startTime: [
        {
            required: true,
            message: '请选择开始时间',
            trigger: ['blur']
        }
    ],
    endTime: [
        {
            required: true,
            message: '请选择结束时间',
            trigger: ['blur']
        }
    ],
    sort: [
        {
            required: true,
            message: '请输入排序',
            trigger: ['blur']
        }
    ],
    isDisable: [
        {
            required: true,
            message: '请选择状态',
            trigger: ['blur']
        }
    ],
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await slotEdit(data) : await slotAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await slotDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
